unit Keywords;

{*
Keywords manager
In fpdocp documentation, you can place special keywords in square blackets [keyword] and this [keyword]
will be replaced with the word specified in special keywords file specified by -k switch
}

interface

uses SysUtils, Classes, Utils;

type
  TKeywordsList = class(TStringList) //* Object for loading and replacing keywords
    function ReplaceKeywords(AStr : string) : string;
  end;

var
  KeywordsList : TKeywordsList; //* Global variable of keywords and their actual meaning

implementation

uses StrUtils;

{ TKeywordsList }

function TKeywordsList.ReplaceKeywords(AStr: string): string;
{*
Replace all keywords in string AStr with keywords values
}
var i : integer;
begin
  result := AStr;
  if pos('[',AStr) <= 0 then
    exit;
  try
    for i := 0 to Count-1 do
      if i mod 2 = 0 then
        AStr := AnsiReplaceStr(AStr,Strings[i],Strings[i+1]);
  except
    on e:Exception do
    begin
      writeln('error: TKeywordsList.ReplaceKeywords("',AStr,'") .. ',e.Message);
      raise;
    end;
  end;
  result := AStr;
end;

initialization

  KeywordsList := TKeywordsList.Create;
  if ParamSwitchExists('-k') then
    KeywordsList.LoadFromFile(ParamSwitchValue('-k'));

finalization

  KeywordsList.Free;

end.
